<?php

/*
 * EachGo电商网站 新媒体
 * www.eachgo.cn
 */

namespace Home\Controller;

use Think\Controller;

class UserController extends Controller {

    public function isUser() {
        if(!empty($_POST)) {
            $user = D('User');
            $result = $user->checkUserPhone($_POST['phone']);
            if(empty($result)) {
                $json['status'] = 0;
                $json['info'] = '该账号不存在！';
            } else {
                $json['status'] = 1;
                $json['info'] = $result[0]['usernickname'].'欢迎您回来！';
            }
            echo json_encode($json);
        }
    }

    public function login() {
        if(!empty($_POST)) {
            $user = D('User');
            $result = $user->isUser($_POST);
                if(!empty($result)) {
                    session("UserInfo",$result);
                    $json['status'] = 1;
                    $json['info'] = '登录成功！欢迎回来！';
                    $json['url'] = U('/Home/Index/index');
                } else {
                    $json['status'] = 0;
                    $json['info'] = "密码和账号不匹配！请重新输入！";
                }
            echo json_encode($json);
        }else{
            $this->display('login');
        }
    }
    public function index(){
        $result=session("UserInfo");
        $school = D('School');
        $SchoolInfo=$school->queryById($result['school_id']);
        if($result['userGender']==true){
            $result['userGender']='男';
        }else{
            $result['userGender']='女';
        }
        session("UserInfo",$result);
        $Address=D("shippingAddress");
        $shippingAddress=$Address->queryAddressByUserId($result['phonenumber']);
        $defaultAddress=$shippingAddress[0];
        $LuckyMoney=D('LuckyMoney');
        $money=$LuckyMoney->sumAll($result['id']);
        $order=D('OrderInfo');
        $orderNumber=$order->getNumberBuy($result['id']);
        $lottery=D('Lottery');
        $lotteryNumber=$lottery->queryNumber($result['id']);
        $return=D('ReturnInfo');
        $returnNumber=$return->queryNumber($result['id']);
        session('returnNumber',$returnNumber[0]['number']);
        session('lotteryNumber',$lotteryNumber[0]['number']);
        session('orderNumber',$orderNumber);
        session('luncyMoney',$money);
        session("defaultAddress",$defaultAddress);
        session("schoolName",$SchoolInfo[0]['schoolname']);
        $this->display();
    }
    public function queryAllUser() {
        $user = D('User');
        $result = $user->queryAll();
        var_dump($result);
    }

    public function checkPhoneNumber() {
        $user = D('User');
        $phoneNumber = '13459876234';
        $Email = '1234567@qq.com';
        $result=$user->queryByPhone($phoneNumber);
       //  $result = $user->checkUserMail($Email);
        var_dump($result);
        if (!$result) {
            echo "手机号可以使用";
        } else {
            echo "手机号已存在";
        }
    }

    public function regist() {
        $school = D('School');
        $schoolName = array('成都大学');
        $SchoolResult = $school->queryLikeByName($schoolName);
        $SchoolResult = $SchoolResult[0];
        var_dump($SchoolResult);
        $data = array(
            'phoneNumber' => '13299090909',
            'userPassword' => '123456',
            'userLotteryNumberInfo_id' => 1,
        );
        $data = array('shool_ID' => (int) $SchoolResult['id']) + $data;
        $user = D('personalcenter');
        $isExist = $user->queryByPhone($data['phoneNumber']);
        //var_dump($isExist);
        if (!$isExist) {
             $UserResult=$user->addUser($data);
            echo '亲，你已经注册成功咯，马上为您自动调整到首页哟！';
        } else {
            echo '亲，手机号已被注册咯，麻烦改一个号码';
        }
    }

    public function updateUser() {
        $user = D('User');
        $data = array(
            'ID' => 2,
            'school_ID' => 32748,
            'phoneNumber' => '13459876234',
            'entersSchoolDate' => '2013-09-12',
            'schoolYear' => 6,
            'userNickName' => '世界那么大',
            'realName' => '王三',
            'userPassword' => '123456',
            'userGender' => 0,
            'userWeibo' => '2354896712@qq.com',
            'userEmail' => '2354896712@qq.com',
            'userCreateTime' => '1970-01-01',
            'latestLoginTime' => '1970-01-01',
            'userLotteryNumberInfo_id' => 1,
        );
        $result = $user->modifyUserInfo($data);
        var_dump($result);
    }

    public function updatePassword() {
        $user = D('User');
        $password = '234234';
        $result = $user->modifyPassword(2, $password);
        var_dump($result);
    }
    public function getAllLotterNum(){
        $lotter=D('UserLotteryNumberInfo');
        $result=$lotter->queryAll();
        var_dump($result);
    }
    public function addLotter(){
        $lotter=D('UserLotteryNumberInfo');
        $data=array(
            'luckyMoney'=>4,
            'oneLottery'=>3,
            'twoLottery'=>3,
            'fiveLottery'=>3,
            'tenLottery'=>3,
            
        );
        $result=$lotter->addLotterNumber($data);
        var_dump($result);
    }
    public function updateNumber(){
        $lotter=D('UserLotteryNumberInfo');
        $result=$lotter->updateLotterNumber(1,1,79);
        var_dump($result);
    }

    /*
     * 2015-08-12：用户退出
     */
    public function logout() {
        if(!empty($_SESSION)) {
            session_unset();
            session_destroy();
            redirect(U("/Home/Index"));
        }
    }

}
